package work.gac.unbind;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import work.gac.unbind.v2.MediaAccount;
import work.gac.unbind.v2.Setting;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Gong.Yang
 */
@Slf4j
public class RequestTest {
    static private final String queryType = "mobile";
    static private final String queryValue = "18697196815";
    static private final String dataLakeToken = "bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2OTExMTY1MTAsInVzZXIiOnsiZW1haWwiOiJkZW1vNjVAZXhhbXBsZS5jb20iLCJ1c2VybmFtZSI6ImxpYW53ZWlnYWNtaW5pIiwicm9sZXMiOltdfSwidXNlcl9uYW1lIjoibGlhbndlaWdhY21pbmkiLCJqdGkiOiJmYTlmYWQ4ZC03NWVjLTQ1MTgtYWRkMi0zZmYzYTgxMGJlODQiLCJjbGllbnRfaWQiOiJsaWFud2VpX2dhY19taW5pIiwic2NvcGUiOlsiYWxsIl19.gC584g5dtyeqFoxiqLkT2ow3VKUk6Z6mx-TeZgZU9xV_n-hwUkVkhDjwojANFbJgMpaVLFIG9Jj_t110GfRbt_DWB8C--id7pOQV-sJNHL6aFIgqnG6jLDC1M9phDMnG-QGOYPB13y8x6g5MMVPpO6sWb9IGPjDiZgMU0-l3-XDivSpKTPFZr1Uvbhmix1Ye_8lPM42tb6KjBkjUnFvVRm2bDHb0X_tvxiVk7PRNwyt4iZxlhqi3vKLOIhx2PyXB4fPJWdTQW-ica3Y1h6fTHH8DM0HS0L5vNo2orMJ4BdDpa7yUjCgkCogqWXRa9c9BoRchj0Zvyk8Ib8zTko30Eg";

    public static void main(String[] args) {
        String url = String.format(Setting.URL.DATA_LAKE_QUERY_MEMBER_INFO, Setting.url);
        JSONObject requestBody = new JSONObject();
        requestBody.set("Brand_code", Setting.brandCode);
        requestBody.set("Program_code", Setting.programCode);
        requestBody.set("queryType", queryType);
        requestBody.set("Value", queryValue);

        Map<String, String> requestHeader = new HashMap<>(16);
        requestHeader.put("ruid", JSONUtil.toJsonStr(new JSONObject().set("uuid", StrUtil.uuid())));
        requestHeader.put("Content-Type", "application/json; charset=utf-8");
        requestHeader.put("Authorization", dataLakeToken);

        String body = HttpRequest.post(url).addHeaders(requestHeader).timeout(10000).body(JSONUtil.toJsonStr(requestBody)).execute().body();
        System.out.println(body);
        JSONObject response = JSONUtil.parseObj(body);
        if (StrUtil.isNotBlank(response.get("error", String.class))) {
            log.info("异常:{}", response);
            return;
        }
        if (response.get("code", Integer.class) != 0) {
            log.info("异常code message:{}", response.get("msg"));
            return;
        }

        //JsonUtil
        JSONArray mediaAccounts = response.getJSONObject("data").getJSONArray("media_account");
        List<MediaAccount> list = new ArrayList<>();
        for (int i = 0; i < mediaAccounts.size(); i++) {
            MediaAccount item = mediaAccounts.get(i, MediaAccount.class);
            list.add(item);
        }
        System.out.println(list);

        if (CollUtil.isEmpty(list)) {
            return;
        }
        MediaAccount mediaAccount = list.get(0);

        //校验
        if (!mediaAccount.getAccountNo().startsWith("oK")) {
            log.info("【解绑】非openId，mediaAccount->{}", JSONUtil.toJsonStr(mediaAccount));
            return;
        }
        if (mediaAccount.getBindStatus() != 1) {
            log.info("【解绑】非绑定会员，mediaAccount->{}", JSONUtil.toJsonStr(mediaAccount));
            return;
        }

        //解绑body
        JSONObject unbindBody = new JSONObject();
        unbindBody.set("channel", Setting.miniChannelCode);
        unbindBody.set("brand_code", Setting.brandCode);
        unbindBody.set("program_code", Setting.programCode);
        unbindBody.set("type", "0");// 1:绑定；0:解绑

        //解绑params
        JSONObject params = new JSONObject();
        params.set("mobile", queryValue);
        params.set("open_id", mediaAccount.getAccountNo());
        unbindBody.set("params", params);

        //解绑url
        String unbindUrl = String.format(Setting.URL.DATA_LAKE_UNBIND, Setting.url);
        Map<String, String> unbindUrlRequestHeader = new HashMap<>(16);
        unbindUrlRequestHeader.put("ruid", JSONUtil.toJsonStr(new JSONObject().set("uuid", StrUtil.uuid())));
        unbindUrlRequestHeader.put("Content-Type", "application/json; charset=utf-8");
        unbindUrlRequestHeader.put("Authorization", dataLakeToken);

        HttpRequest.post(unbindUrl).addHeaders(unbindUrlRequestHeader)
                .timeout(10000).body(JSONUtil.toJsonStr(unbindBody)).execute().body();

    }
}
